import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class JrDialog {
  static void showSimpleDialog(String title, String content) {
    Get.dialog(
      AlertDialog(
        title: Text(title),
        content: Text(content),
        actions: <Widget>[
          TextButton(
            child: const Text('OK'),
            onPressed: () {
              Get.back();
            },
          ),
        ],
      ),
    );
  }

  static Future<bool> showConfirmDialog(String title, String content) async {
    bool confirmed = false;
    await Get.dialog(
      AlertDialog(
        title: Text(title),
        content: Text(content),
        actions: <Widget>[
          TextButton(
            child: const Text('Cancel'),
            onPressed: () {
              confirmed = false;
              Get.back();
            },
          ),
          TextButton(
            child: const Text('Confirm'),
            onPressed: () {
              confirmed = true;
              Get.back();
            },
          ),
        ],
      ),
    );
    return confirmed;
  }

  static void showCustomDialog(Widget Function(BuildContext) builder) {
    Get.dialog(
      Dialog(
        child: Builder(
          builder: builder,
        ),
      ),
    );
  }

  static Future<DateTime?> showDatePicker({
    required DateTime initialDateTime,
    DateTime? maximumDate,
    Function(DateTime value)? onDateTimeChanged,
  }) async {
    DateTime select = initialDateTime;
    return showCupertinoModalPopup(
      context: Get.context!,
      builder: (ctx) {
        return Container(
          height: 300,
          color: Colors.white,
          child: Column(
            children: [
              SizedBox(
                height: 55,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    CupertinoButton(
                      child: const Text('Cancel'),
                      onPressed: () {
                        Get.back();
                      },
                    ),
                    CupertinoButton(
                      child: const Text('Confirm'),
                      onPressed: () {
                        onDateTimeChanged?.call(select);
                        Get.back();
                      },
                    ),
                  ],
                ),
              ),
              Expanded(
                child: CupertinoDatePicker(
                  maximumDate: maximumDate,
                  initialDateTime: initialDateTime,
                  mode: CupertinoDatePickerMode.date,
                  onDateTimeChanged: (DateTime value) {
                    select = value;
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  static void showSelectDialog(List menus) {
    showCupertinoModalPopup(
      context: Get.context!,
      builder: (context) {
        List<Widget> children = [];
        for (var item in menus) {
          children.add(CupertinoActionSheetAction(
            onPressed: () async {
              Navigator.pop(context);
              item['onPressed']?.call();
            },
            child: Text(item['title']),
          ));
        }

        return CupertinoActionSheet(
          actions: children,
          cancelButton: CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Cancel'),
          ),
        );
      },
    );
  }
}